//
//  AppDelegate.swift
//  GZWeibo009
//
//  Created by gzxzmac on 16/3/19.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        setAppearance()
        
        // TODO: 测试加载账号
        let userAccount = CZUserAccountViewModel.sharedInstance.userAccount
        print("AppDelegate userAccount: \(userAccount)")
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 设置根控制器
        window?.rootViewController = defaultViewController()
        
        window?.makeKeyAndVisible()
        
        CZSQLiteManager.sharedManager
        
        setupAFNetworking()
        
        return true
    }
    
    private func setupAFNetworking() {
        let urlCache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        NSURLCache.setSharedURLCache(urlCache)
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
    }
    
    /**
     界面切换,不需要介入,直接可以判断的
     
     - returns: 默认的控制器
     */
    private func defaultViewController() -> UIViewController {
        if !CZUserAccountViewModel.sharedInstance.userLogin {
            // 没有登陆
            return CZMainViewController()
        }
        
        // 登陆过了
        return isNewVersion() ? CZNewFeatureViewController() : CZWelcomeViewController()
    }
    
    /// 界面切换统一调用这个方法,到时候如果界面切换出现问题,直接在这个方法里面打断点,方便调试
    /// 外面使用需要拿到对象在调用,比较麻烦
    private func switchRootViewController(controller: UIViewController) {
        window?.rootViewController = controller
    }
    
    /// 外部调用的切换根控制器方法
    class func outSwitchRootViewController(controller: UIViewController) {
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.switchRootViewController(controller)
    }
    
    /**
     是否是新版本
     
     - returns: 是否是新版本
     */
    private func isNewVersion() -> Bool {
        // 1.获取当前版本
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
//        CZPrint("version: \(version)")
        
        // 2.获取上一次的版本
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
        
//        CZPrint("sandboxVersion: \(sandboxVersion)")
        
        // 3.对比, 不一样就是新版本
        let newVersion = version != sandboxVersion
        
        // 4.是新版本,保存当前版本号
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        // 5.返回比较结果
        return newVersion
    }
    
    /// 设置皮肤
    private func setAppearance() {
        // 设置导航栏皮肤
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        CZStatusDAL.sharedDAL.clearCacheData()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

